package models;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import play.db.ebean.Model;

@Entity
@Table(name = "settings")
public class Setting extends Model {

	private static final long serialVersionUID = 1L;

	public static final String CUSTOMERS_ROOT_DIRECTORY = "customers-root-directory";
	public static final String TEST_EXECUTION_URL = "test-execution-url";

	
	@Column(name = "name")
	@Id
	private String name;

	@Column(name = "type")
	private SettingType type;

	@Column(name = "description")
	private String description;

	@Column(name = "value_boolean", nullable = true)
	private boolean booleanValue;

	@Column(name = "value_float", nullable = true)
	private float floatValue;

	@Column(name = "value_int", nullable = true)
	private int intValue;

	@Column(name = "value_string", nullable = true)
	private String stringValue;

	public static Finder<String, Setting> find = new Finder<String, Setting>(String.class, Setting.class);

	public Setting(String name) {
		this.name = name;
	}

	public Setting(String name, Object value) {
		this(name);
		setValue(value);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public SettingType getType() {
		return type;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Object getValue() {
		Object value = null;
		switch (type) {
			case Boolean:
				value = booleanValue;
				break;
			case Float:
				value = floatValue;
				break;
			case Integer:
				value = intValue;
				break;
			case String:
				value = stringValue;
				break;
		}
		return value;
	}

	public void setValue(Object value) {
		SettingType type = SettingType.String;
		boolean b = false;
		float f = 0f;
		int i = 0;
		String s = null;
		Class<? extends Object> clazz = value != null ? value.getClass() : null;
		if (clazz != null) {
			if (clazz == Boolean.class) {
				type = SettingType.Boolean;
				b = (Boolean) value;
			} else if (clazz == Float.class) {
				type = SettingType.Float;
				f = (Float) value;
			} else if (clazz == Integer.class) {
				type = SettingType.Integer;
				i = (Integer) value;
			} else {
				type = SettingType.String;
				s = clazz == String.class ? (String) value : value.toString();
			}
		}
		setValue(type, b, f, i, s);
	}

	public boolean getBooleanValue() {
		return booleanValue;
	}

	public void setBooleanValue(boolean value) {
		setValue(SettingType.Boolean, value, 0, 0, null);
	}

	public float getFloatValue() {
		return floatValue;
	}

	public void setFloatValue(float value) {
		setValue(SettingType.Float, false, value, 0, null);
	}

	public int getIntegerValue() {
		return intValue;
	}

	public void setIntegerValue(int value) {
		setValue(SettingType.Integer, false, 0, value, null);
	}

	public String getStringValue() {
		return stringValue;
	}

	public void setStringValue(String value) {
		setValue(SettingType.String, false, 0, 0, value);
	}

	private void setValue(SettingType type, boolean b, float f, int i, String s) {
		this.type = type;
		booleanValue = b;
		floatValue = f;
		intValue = i;
		stringValue = s;
	}

	@Override
	public String toString() {
		return "Setting [name=" + name + ", type=" + type + ", description=" + description + "]";
	}

}
